<?php

namespace hiduoke\services\wechatMinApp;

use app\common\model\open\Open;
use app\common\model\open\OpenTemplate;
use app\common\model\platform\PlatformApp;
use app\common\model\platform\PlatformAppOpen;
use EasyWeChat\Factory;
use hiduoke\services\wechatMinApp\API\WechatOpenApi;

use think\Exception;
use think\facade\Cache;

/**
 * Notes: 抖音服务商方法类
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 * Created on:  2024/3/14 15:57
 */
class WechatMinAppServer
{
    /**
     * 抖音服务商接口
     * @var \EasyWeChat\OpenPlatform\Application
     */
    protected $WechatOpenAPI;
    /**
     * 小程序ID
     * @var int
     */
    public $platform_app_id;


    /**
     * @var OpenTemplate
     */
    public $open_template;

    /**
     * @var Open|array|mixed|\think\Model|null
     */
    public $open;

    /**
     * 抖音小程序
     * @var PlatformApp|array|mixed|\think\Model|null
     */
    protected $platform_app;
    /**
     * 服务商平台
     * @var PlatformAppOpen|array|mixed|\think\Model|null
     */
    protected $platform_app_open;

    public $config;
    /**
     * 授权小程序 appid
     * @var mixed
     */
    public $authorization_appid;
    /**
     * 应用标识
     * @var mixed
     */
    public $addon_name;
    /**
     * 第三方小程序应用接口调用凭据
     * @var mixed
     */
    public $component_access_token;
    public $plaid;

    public $err_msg;
    public $err_code;


    public function __construct($platform_app_id)
    {
        $this->platform_app_id = $platform_app_id;
        $this->platform_app = (new PlatformApp())->where(['id' => $platform_app_id])->find();
        $this->plaid = $this->platform_app['plaid'];
        $this->addon_name = $this->platform_app['addon_name'];
        $this->open_template = (new OpenTemplate())->where(['addon_name' => $this->addon_name])->find();
        $this->open = $this->open_template->open;
        $this->config = $this->open['config'];
        $this->WechatOpenAPI = Factory::openPlatform($this->config);
    }


    public function miniProgram()
    {
        return $this->WechatOpenAPI->miniProgram($this->platform_app['appid'], $this->platform_app['server_config']['authorizer_refresh_token']);
    }

    public function access_token()
    {
        return $this->miniProgram()->access_token->getToken();
    }

//
//    // 获取二维码
//    public function getQrcode($version, $path = '')
//    {
//        $res = $this->WechatOpenAPI->getQrcode($this->get_authorizer_access_token(), $version, $path);
//        return $res;
//    }
//
//
//    /**
//     * 登录
//     * Create by :  刘泓宾<514660094@qq.com>
//     * @param $code
//     * @param $anonymous_code
//     * @return mixed
//     */
//    public function login($code, $anonymous_code = '')
//    {
//        return $this->WechatOpenAPI->login($this->get_authorizer_access_token(), $code, $anonymous_code);
//    }
//
//
//    public function info()
//    {
//        return $this->WechatOpenAPI->info($this->get_authorizer_access_token());
//    }
//
//    /******************其他助手函数**************************/
//
//
    /**
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \EasyWeChat\OpenPlatform\Application
     */
    public function getOpenApi()
    {
        return $this->WechatOpenAPI;
    }

    /**
     * @return PlatformApp|array|mixed|\think\Model|null
     */
    public function getPlatformApp(): mixed
    {
        return $this->platform_app;
    }

    /**
     * @return PlatformAppOpen|array|mixed|\think\Model|null
     */
    public function getPlatformAppOpen(): mixed
    {
        return $this->platform_app_open;
    }
}
